<template>
    <div>
          <!-- <button class="" @click="downloadReport()">下载</button> -->
      <!-- <web-view v-bind:src="path"></web-view> -->
    </div>
</template>
<script>
import s from '@/utils/s'
import { showLoad } from '@/utils/index'
import storage from '@/utils/storage'
export default {
  data () {
    return {
      id: ''
    }
  },
  onLoad (option) {
    console.log('haha', option)
    this.downloadReport(option.id)
  },
  onShow () {
    // const id = this.$root.$mp.query.id
    // this.id = this.$root.$mp.query.id
  },
  methods: {
    // 获取报告详情
    reportDetail (id) {
      wx.request({
        url: s.prefixHttp + `report-service/api/report/detail/${id}`,
        method: 'GET',
        header: {'token': storage.get('token')},
        success: function (res) {
          const data = res.data.data
          console.log(data)
        },
        fail: function (res) {

        }
      })
    },
    // 点击下载报告
    downloadReport (id) {
      const $this = this
      showLoad('加载中')
      wx.request({
        url: s.prefixHttp + `report-service/api/report/download/${id}`,
        method: 'GET',
        header: {'token': storage.get('token')},
        success: function (res) {
          const data = res.data.data
          const path = data.path
          $this.createPdf(path)
        },
        fail: function (res) {
          return false
        }
      })
    },
    // 生成pdf
    createPdf (tempFilePath) {
      wx.downloadFile({
        url: tempFilePath,
        header: {'token': storage.get('token')},
        success (res) {
          console.log(res)
          const tempFilePath = res.tempFilePath
          if (res.statusCode === 200) {
            wx.openDocument({
              filePath: tempFilePath,
              success: function (res) {
                wx.hideLoading()
                console.log('打开文档成功')
              }
            })
          }
        }
      })
    }
  }
}
</script>